<template>
  <div class="loginBody">
    <div class="left">
      <img src="@/assets/img/logo2.png" />
      <!-- <img src="@/assets/img/logo.png" /> -->
    </div>
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">登录账户</h1>
        <el-form :model="loginForm" label-width="100px" :rules="rules" ref="loginForm">
          <el-form-item label="账号" prop="no">
            <el-input style="" type="text" v-model="loginForm.no" autocomplete="off" size="samll" placeholder="请输入你的账号">
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input style="" type="password" v-model="loginForm.password" show-password autocomplete="off" size="samll" placeholder="请输入你的密码"
              @keyup.enter.native="confirm">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button class="l1" @click="confirm" :disabled="confirm_disabled" round>立即登录</el-button><br>
            <!-- <el-button class="r1" @click="register()" round>没有账户?&nbsp;&nbsp;&nbsp;立即注册</el-button> -->
            <span @click="register()" style="margin-left: 150px;">没有账户?&nbsp;立即注册</span>
            <!-- <span style="color: rgb(7, 7, 7);margin-left: 80px;font-size: 15px;" @click="handleForgetPass">忘记密码</span> -->
          </el-form-item>
        </el-form>
      </div>
    </div>


    <el-dialog title="忘记密码" :visible.sync="forgetPassDialogVis" width="30%">
      <el-form :model="forgetUserForm" ref="forgetUserForm" label-width="80px" style="padding-right: 20px;">
        <el-form-item label="账号">
          <el-input v-model="forgetUserForm.no" autocomplete="off" placeholder="请输入你的账号"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="forgetUserForm.password" autocomplete="off" placeholder="请输入你的密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="forgetUserForm.chanPassword" autocomplete="off" placeholder="请再次确认密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="forgetPassDialogVis = false">取 消</el-button>
        <el-button type="primary" @click="resetPassword">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      confirm_disabled: false,
      forgetPassDialogVis: false,
      forgetUserForm: {},        //忘记密码的表单数据
      loginForm: {
        no: '',
        password: '',
        roleId: '',
      },
      rules: {
        no: [
          { required: true, message: '请输入账号', trigger: 'blur' }

        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    //初始化表单的数据
    handleForgetPass() {
      this.forgetUserForm = {}
      this.forgetPassDialogVis = true
    },
    //忘记密码
    resetPassword() {
      this.$refs.forgetUserForm.validate((valid) => {
        if (valid) {  //valid成功为true,失败为false
          //去后台验证用户密码
          this.$axios.post(this.$httpUrl + '/user/forgetPass', this.forgetUserForm).then(res => res.data).then(res => {
            console.log(res)
            // if (this.forgetUserForm.password === this.forgetUserForm.chanPassword) { }
            if (res.code == 200) {
              this.$message.success('修改成功')
              this.forgetPassDialogVis = false;
            }

            else {
              this.$message.error(res.msg)
            }
          });
        }
      });
    },

    confirm() {
      this.confirm_disabled = true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {  //valid成功为true,失败为false
          //去后台验证用户密码
          this.$axios.post(this.$httpUrl + '/user/login', this.loginForm).then(res => res.data).then(res => {
            console.log(res)
            if (res.code == 200) {
              //存储
              sessionStorage.setItem("CurUser", JSON.stringify(res.data))
              //跳转到主页
              this.$router.replace('VsHome');

            } else {
              this.confirm_disabled = false;
              alert('校验失败，用户名或密码错误！');
              return false;
            }
          });
        } else {
          this.confirm_disabled = false;
          console.log('校验失败');
          return false;
        }
      });
    },
    register() {
      this.$router.replace('Register');
    }
  }
}
</script>

<style scoped>
.element.style {
    width: 300px;
}

img {
    width: 300px;
    margin-left: 200px;
    margin-top: 150px;
    /* border: 1px solid black; */

}
.left{
    width: 100px;
    height: 200px;
}
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url(@/assets/img/bj6.jpg) no-repeat;
  /* background-color: #B3C0D1; */
}

.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  /* margin-left: -200px; */
  width: 500px;
  height: 330px;
  background: #fff;
  border-radius: 5%;
  background-color: rgba(226, 234, 236, 0.7);
  /* 蓝色背景，50% 透明 */
  /* backdrop-filter: blur(2px) brightness(95%); */
  box-shadow: 0px 0px 30px -10px #0a1032;
}

.login-title {
  margin: 20px 0;
  margin-left: 60px;
  color: rgb(31, 32, 33);
  text-align: center;
  /* border: 1px solid black; */
}

.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;


}

.l1 {
  background-color: #0a1032;
  color: aliceblue;
  width: 300px;
}

.r1 {
  background-color: #0a1032;
  color: aliceblue;
  width: 300px;
  margin-top: 10px;
}
</style>